<?php
/*
 * Function:
 * Project :SuperEMall
 *
 * Dreamxyp(QQ:31996798) - GZ - PHP - buy.php
 * Coding  :xiayiping[xiayiping_xyp@163.com]
 * Created on 2008-2-4
 */
require '../inc.php';
if(!urs)
{
    g::gowin(surl::get('www')."login.php?ourl=".urlencode(surl::url()));
    exit();   
}
class PObject
{
    public function payOK()
    {
        if(authimg::de() == $_POST["verifycode"])
        {
	        global $scfg;
	        $AddressId  = $_POST["AddressId"];
	        //加入新的地址
	        if(empty($AddressId))
	        {
	            $db_member 	= new db($scfg['db']['member']);
	            $bind = array(
					"urs"=>urs,
					"real_name"=>$_POST["real_name"],
					"country"=>$_POST["country"],
					"address_province"=>$_POST["address_province"],
					"address_city"=>$_POST["address_city"],
					"address_borough"=>$_POST["address_borough"],
					"address"=>$_POST["address"],
					"zip"=>$_POST["zip"],
					"tel"=>$_POST["tel"]
				);
				$AddressId = $db_member->insert("member_addr",$bind);
				!$AddressId && $AddressId = $db_member->insert("member_addr",$bind);
	        }
	        $db_goods 	= new db($scfg['db']['goods']);
	        $arr	    = pct::de($_POST['id']);
		    $pct_url	= pct::url($_POST['id']);
		    $table      = "goods_info";
		    $where      = array(
				"where"=>" where gYmd =:gYmd and gid =:gid ",
				"bind" =>$arr
			);
			$time		= time();
			$goods      = $db_goods->row("select * from {$table} {$where['where']} ",$where['bind']);
	       // print_r($goods);
	        if($goods["urs"] == urs)
	        {
	            echo g::msg("提示:自己不能买自己的商品!");
	            g::gowin($pct_url);
	        }elseif($goods["cSellType"] != 1)
	        {
	            echo g::msg("提示:本商品为拍卖商品请拍卖出价!");
	            g::gowin($pct_url);
	        }elseif($goods["uploadDate"]>$time)
	        {
	            echo g::msg("提示:本商品还没开始出销!");
	            g::gowin($pct_url);
	        }elseif($goods["dwValidDurationExt"]<$time)
	        {
	            echo g::msg("提示:本商品因到期已下架!");
	            g::gowin($pct_url);
	        }elseif($goods["dwNum"]-$goods["dwNumSell"]<$_POST["dwOpNum"])
	        {
	            if($goods["dwNum"]-$goods["dwNumSell"] <=0)
	            {
	            	echo g::msg("提示:本商品已没存货!");
	            }else
	            {
	                echo g::msg("提示:商品货存不够{$_POST['dwOpNum']}!");
	            }
	            g::gowin();
	        }else
	        {	        
		        $bind 		= array(
		        	"oYmd"		=>date("Ymd"),
		        	//"oid"		=>"",//这项不要
		        	"gYmd"		=>$arr["gYmd"],
		        	"gid"		=>$arr["gid"],
		        	"cSellType" =>1,
		        	"AddressId" =>$AddressId,
		        	"ursSell"	=>$goods["urs"],
		        	"ursBuy"	=>urs,
		        	"Price"		=>$goods["dwPrice_bin"],
		        	"MailPrice" =>$_POST["MailType"]?$goods[$_POST["MailType"]]:0,//MailPrice
		        	"MailType"	=>$_POST["MailType"],
		        	"sales"		=>0,
					"dwOpNum"   =>$_POST["dwOpNum"],
		        	"BuyerNote" =>$_POST["BuyerNote"],
		        	"gSavaTime" =>$time,
		        	"gIp"		=>g::ip(),
		        	"estate"	=>1
		        );
		        $oid = $db_goods->insert("goods_buy",$bind);
		        if($oid)
		        {
		            g::gowin("?act=buyOk&id={$_POST['id']}");
		            //$Object = new Object();
		            //$Object->buyOk($goods);
		        }else
		        {
		            echo g::msg("提示:系统忙请稍后再试!");
            		g::gowin();
		        }
	        }	        
        }else
        {
            echo g::msg("提示:验证码有误!");
            g::gowin();
        }
    }
    public function  state()
    {
        global $scfg; 
    }
}
class Object
{
    public function check()
    {
        global $scfg;
        //商品信息
        $db_goods = new db($scfg['db']['goods']);	
		$arr	  = pct::de($_GET['id']);
	    $table    = "goods_info";
		$where    = array(
			"where"=>" where gYmd =:gYmd and gid =:gid ",
			"bind" =>$arr
		);
		$rs 	  	= $db_goods->row("select * from {$table} {$where['where']} ",$where['bind']);
		$img        = pct::img($rs["gYmd"],$rs["gid"]);
		$url 		= pct::url($rs["gYmd"],$rs["gid"]);
		$dwOpNum    = $rs["dwOpNum"]?$rs["dwOpNum"]:$rs["dwNum"]-$rs["dwNumSell"];
		//收件地址
		$db_member 	= new db($scfg['db']['member']);
		$where = array(
			"str" => " where urs = ? ",
			"bind"=>urs
		);
		$member_addr = $db_member->dataArray("select * from member_addr {$where['str']} limit 0,20",$where['bind']);
		tpl::assign('id',$_GET['id']);
		tpl::assign('member_addr',$member_addr);
		tpl::assign("url",$url);
		tpl::assign("dwOpNum",$dwOpNum);
		tpl::assign("img",$img);
		tpl::assign("rs" ,$rs);
		if($rs["cSellType"] == 1)
		{
		    tpl::display("my/buy.check.fixup.my.tpl.php");
		}else
		{
		    tpl::display("my/buy.check.drift.my.tpl.php");
		}		
    }
    public function buyOk($rs=false)
    {
        if(!$rs)
        {
            global $scfg;
            $db_goods 	= new db($scfg['db']['goods']);
	        $arr	    = pct::de($_REQUEST['id']);
		    $table      = "goods_info";
		    $where      = array(
				"where"=>" where gYmd =:gYmd and gid =:gid ",
				"bind" =>$arr
			);
			$rs      = $db_goods->row("select * from {$table} {$where['where']} ",$where['bind']);
	    }
	    tpl::assign("rs" ,$rs);
        tpl::display("my/buy.buyOk.my.tpl.php");
    }
    public function payOk()
    {
        tpl::display("my/buy.payOk.my.tpl.php");
    }
    public function complete()
    {
        tpl::display("my/buy.complete.my.tpl.php");
    }
}
$Pact = $_REQUEST["Pact"];
if($Pact)
{
    route("PObject",$Pact);
    exit();
}
//路由
$act = $_REQUEST["act"];
$act && route("Object",$act);
?>
